<?php
declare (strict_types=1);
namespace app\admin\model\site\discovery;

use app\common\logic\Attach;
use app\common\model\BaseModel;
use think\model\relation\BelongsTo;
use think\model\relation\HasOne;

/**
 * 定义发现页模型
 */
class Discovery extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'site_discovery_config';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    // 定义全局的查询范围
    protected $globalScope = ['open'];

    public function scopeOpen($query)
    {
        $query->where('open', 1);
    }

    public function iconUrl(): BelongsTo
    {
        return $this->belongsTo(Attach::class, 'icon', 'id')->bind(['icon_url' => 'fileurl']);
    }

    public function groupTitle(): BelongsTo
    {
        return $this->belongsTo(DiscoveryGroup::class, 'group_id')->bind(['group_title' => 'title']);
    }

    public function discoveryData(): HasOne
    {
        return $this->hasOne(DiscoveryData::class, 'config_id', 'id')->with(['iconUrl']);
    }
}
